﻿#include <algorithm>
#include <bitset>
#include <iostream>
#include <vector>

#include "ZRingBuffer.h"
#include "ZTimerSW.h"

using namespace std;

int main() {
  std::cout << "Test Start" << std::endl;

  /* 测试寻找指定值 */
  ZRingBuffer ringBuffer(10240);
  for (int i = 0; i < ringBuffer.capacity(); i++) {
    ringBuffer.push_back(i % 255);
  }
  auto f0 = ringBuffer.indexOf(240);
  std::cout << "indexOf(240): " << f0 << std::endl;

  auto f1 = ringBuffer.indexOf(240, 300);
  std::cout << "indexOf(240, 300): " << f1 << std::endl;

  std::vector<uint8_t> data{240, 241, 242, 243, 244};
  auto f2 = ringBuffer.indexOf(data.data(), data.size());
  std::cout << "indexOf(data): " << f2 << std::endl;

  /* 测试环形buffer基础 */
  /*
  static uint8_t buffer[10 * 1024 * 1024];
  ZRingBuffer ringBuffer(buffer, sizeof(buffer));
  auto t0 = ZTimerSW::curTime();
  for (int i = 0; i < ringBuffer.capacity(); i++) {
    ringBuffer.push_back(i % 255);
  }
  auto t1 = ZTimerSW::curTime();
  std::cout << "push_back cost: " << (t1 - t0) << "ms" << std::endl;
  unsigned total{0};
  for (int i = 0; i < ringBuffer.size(); i++) {
    total += ringBuffer[i];
  }
  auto t2 = ZTimerSW::curTime();
  std::cout << "read cost: " << (t2 - t1) << "ms" << std::endl;
  auto ptr{ringBuffer.data()};
  for (int i = 0; i < ringBuffer.size(); i++) {
    total += ptr[i];
  }
  auto t3 = ZTimerSW::curTime();
  std::cout << "read2 cost: " << (t3 - t2) << "ms" << std::endl;
   */

  /* 测试批量读写 */
  /*
  ZRingBuffer ringBuffer1(1024 * 1024);
  auto t10 = ZTimerSW::curTime();
  // 每次写入数据,再读取数据
  auto testNum = 10;
  std::vector<uint8_t> testData(testNum);
  uint8_t tempBuffer[1024]{};
  for (int i = 0; i < ringBuffer1.capacity(); ++i) {
    // 写入的值
    auto writeValue = i % 255;
    std::fill(testData.begin(), testData.end(), writeValue);
    ringBuffer1.push_back(testData.data(), testData.size());
    // for (int j = 0; j < testNum; ++j) {
    // auto v = ringBuffer1.left(testNum);
    ringBuffer1.left(testNum, tempBuffer);
    // std::cout << (int) v << ' ';
    for (int j = 0; j < testNum; ++j) {
      auto tempV{tempBuffer[j]};
      if (tempV != writeValue) {
        std::cerr << "error value!" << '\n';
      }
    }
    // printf("ringBuffer: %d", v);
    ringBuffer1.pop_front(testNum);
    // std::cout << std::endl;
  }
  auto t11 = ZTimerSW::curTime();
  std::cout << "cost: " << (t11 - t10) << "ms" << std::endl;
   */

  /* 测试定时器 */
  /*
  printf("SCU Timer Interrupt Test \r\n");

  Task task{};
  task.name = "a";
  task.callback = hwTimerTest0;
  hwTimer.add(task);
  task.callback = swTimerTest0;
  swTimer.add(task);

  Task task1{};
  task1.name = "b";
  task1.callback = hwTimerTest1;
  task1.period = 2000;
  hwTimer.add(task1);
  task1.callback = swTimerTest1;
  swTimer.add(task1);
  */

  std::cout << "Test Done" << std::endl;

  //  while (true) {
  // swTimer.runOnce();
  //  }
}
